#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "LOG.hpp"
#include "lockgroup.hpp"

using namespace std;
const string defaultpath = "./dict.txt"; 
const string sep = ": ";

class Dict
{
private:
    bool Load()
    {
        //1.打开文件
        ifstream in(_path);
        if(!in.is_open())
        {
            LOG(ERROR,"%s open error",_path);
            return false;
        }
        //2.读取
        string line;
        while(getline(in,line)) //从in 读取内容到line当中
        {
            if(line.empty()) continue;
            auto pos = line.find(sep);    //cat: 猫
            if(pos == string::npos) continue;
            string word = line.substr(0,pos);
            if(word.empty()) continue;
            string han = line.substr(pos+sep.size());
            if(han.empty()) continue;
            _dict[word] = han;
        }
        in.close();
        return true;

    }
public:
    Dict(string path = defaultpath)
    :_path(path)
    {
        Load();
    }
    string Translate(const string& word)
    {
        auto iter = _dict.find(word);
        if(iter == _dict.end())
        {
            return "未找到";
        }
        //return iter->second;
        return _dict[word];

    }
    ~Dict()
    {
    }

private:
    string _path;
    unordered_map<string, string> _dict;
};